package com.example.demo2.config;

import cn.hutool.json.JSONUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.demo2.entity.Customer;
import com.example.demo2.entity.DTO.FieldCustomerDTO;
import com.example.demo2.entity.DTO.FieldShopDTO;
import com.example.demo2.entity.ShopPlace;
import com.example.demo2.exception.ServiceException;
import com.example.demo2.service.CustomerService;
import com.example.demo2.service.ShopPlaceService;

import java.util.ArrayList;
import java.util.List;


public class ExcelHeadShopDataListener extends AnalysisEventListener<FieldShopDTO> {
    private ShopPlaceService shopPlaceService;

    public ExcelHeadShopDataListener(ShopPlaceService shopPlaceService) {
        this.shopPlaceService = shopPlaceService;
    }

    List<ShopPlace> list = new ArrayList<>();

    @Override
    public void invoke(FieldShopDTO fieldShopDTO, AnalysisContext analysisContext) {
        ShopPlace shopPlace = new ShopPlace();
        shopPlace.setName(fieldShopDTO.get商品名称());
        shopPlace.setBigPakePrice(fieldShopDTO.get寄放行李箱价格());
        shopPlace.setMiddlePakePrice(fieldShopDTO.get寄放背包价格());
        shopPlace.setCtiyDescription(fieldShopDTO.get地址描述());
        list.add(shopPlace);
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        try {
            shopPlaceService.saveBatch(list);
        } catch (Exception e) {
            throw new ServiceException("-1", JSONUtil.toJsonStr(e));
        }
    }
}
